package com.hontlong.toy.parrot;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;

final public class DBConnectFactory {

	private static final Logger logger = Logger
			.getLogger(DBConnectFactory.class);

	// private static final String connectionURI =
	// "jdbc:apache:commons:dbcp:/hour41.search";
	private static final String baseConnectionURI = "jdbc:apache:commons:dbcp:/etc/";

	/**
	 * 返回一个已经成功登陆的数据库connection对象，目的是要实现连接和登陆过程的封装，方便以后为完全而更改此连接和登陆方式
	 * 
	 */
	// -Djdbc.drivers=oracle.jdbc.driver.OracleDriver:org.apache.commons.dbcp.PoolingDriver
	// -Djdbc.drivers=com.mysql.jdbc.Driver:org.apache.commons.dbcp.PoolingDriver
	//
	// "jdbc:mysql://localhost/test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&mysqlEncoding=utf8"
	//
	final public static Connection getDBConnection(final String dbName) {

		try {
			return DriverManager
					.getConnection(DBConnectFactory.baseConnectionURI + dbName);
		} catch (final SQLException e) {
			DBConnectFactory.logger.error("", e);
			throw new RuntimeException("与数据库[" + dbName + "]连接时错误：");
		}

	}
}
